package com.slz.crm.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slz.crm.common.untils.IOUtils;
import com.slz.crm.pojo.dto.ApprovalAttachmentDTO;
import com.slz.crm.pojo.entity.ApprovalAttachmentEntity;
import com.slz.crm.pojo.vo.ApprovalAttachmentVO;
import com.slz.crm.server.annotation.Privacy;
import com.slz.crm.server.mapper.ApprovalAttachmentMapper;
import com.slz.crm.server.service.ApprovalAttachmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApprovalAttachmentServiceImpl extends ServiceImpl<ApprovalAttachmentMapper, ApprovalAttachmentEntity> implements ApprovalAttachmentService {
    @Override
    public void saveBatch(List<ApprovalAttachmentDTO> list) {

        for (ApprovalAttachmentDTO dto1 : list) {
            ApprovalAttachmentEntity entity1 = IOUtils.writeFile(dto1);
            baseMapper.insert(entity1);
        }

    }

    @Override
    public void removeByApprovalIds(List<Long> ids) {

        List<ApprovalAttachmentEntity> entities = baseMapper.selectList(new LambdaQueryWrapper<ApprovalAttachmentEntity>()
                .in(ApprovalAttachmentEntity::getApprovalId, ids));
        List<Long> attachmentIds = entities.stream().map(ApprovalAttachmentEntity::getId).collect(Collectors.toList());
        baseMapper.deleteBatchIds(attachmentIds);
        IOUtils.deleteFile(entities);
    }

    @Override
    @Privacy
    public List<ApprovalAttachmentVO> getByApprovalIds(List<Long> approvalIds) {

        List<ApprovalAttachmentEntity> entities = baseMapper.selectList(new LambdaQueryWrapper<ApprovalAttachmentEntity>()
                .in(ApprovalAttachmentEntity::getApprovalId, approvalIds));

        return IOUtils.readFile(entities);
    }
}
